//
// Created by joker on 9/3/19.
//

#include "functional_utils.h"

namespace {
    long mem_limit;

    struct InitLDUtils {

        InitLDUtils() {
#ifdef LD_LINUX
            struct sysinfo inf;
            sysinfo(&inf);
            mem_limit = inf.freeram;
#elif defined(LD_WIN32)
            MEMORYSTATUSEX inf = { 0 };
            inf.dwLength = sizeof(inf);
            GlobalMemoryStatusEx(&inf);
            mem_limit = inf.ullAvailPageFile;
#endif // LD_LINUX

        }
    };

    static InitLDUtils initLdUtils{};
}


double functional_utils::cpu_memory_allocated_ratio() {
#ifdef LD_LINUX
    struct sysinfo inf;
    sysinfo(&inf);

    return 1.0 - (double) inf.freeram / mem_limit;

#elif defined(LD_WIN32)
    //        TODO: Add support for windows
    MEMORYSTATUSEX inf = { 0 };
    inf.dwLength = sizeof(inf);
    GlobalMemoryStatusEx(&inf);
    return  1.0 - (double)inf.ullAvailPageFile / mem_limit;
#endif
}